// Do file that run some heterogeneity analyses
// Figure 3, Tables A7_A8
// Last changed: 2020-04-21 (KOL)
  
  clear all
  set more off 
  set matsize 10000
  
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  log using "`output'\Output\Fig3_TabA6_A7.log", replace text
  
  use "E:\ProjData\PlacementCandidacy\utp-DataMainSample.dta", clear
  
 // Merge on municipality data 
   gen kkod = KommunKod
   
   capture drop _merge
   merge m:1 kkod Ar using "E:\ProjData\UtplaceringsData8594\utp-MunData.dta"
   
  listwise Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr TidI InvAlder logpEnklavVuxenInvAr ///
           mUtb_infodda Herfindahl effectiveparties disproportionality leftshare
  
  
 //################ Main results with municipality controls #################################// 
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  local controls4 "RMKvot mUtb_infodda Herfindahl effectiveparties disproportionality leftshare nydrostkval"
  
  reghdfe Nominerad logpEnklavVuxen `controls4' if `sample', absorb(KommunValAr `controls1') cluster(KommunValAr) old
  eststo c1 
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls4' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo c2
  sum Nominerad if e(sample)==1
   
  reghdfe Nominerad `controls2' logpEnklavVuxen `controls4' if `sample', absorb(`controls1' KommunValAr) cluster(KommunValAr) old
  eststo c3
  sum Nominerad if e(sample)==1
   
  reghdfe Nominerad `controls2' `controls4' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo c4
  sum Nominerad if e(sample)==1
   
  esttab c1 c2 c3 c4 using "`output'\Output\MunContTable2.rtf", keep(`controls2' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
 
//######################### Divide the results by left party strength #################################// 
				
// Regression results Table 				
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  sum leftshare if `sample' , detail
  gen StodV = leftshare > r(p50) if leftshare <.
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodV ==0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodV ==1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo ca2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodV ==0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo ca3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodV ==1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo ca4
  sum Nominerad if e(sample)==1
    
 // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  gen intT = logpEnklavVuxen*StodV
  gen intI = logpEnklavVuxenInvAr*StodV
  
  reghdfe Nominerad  `controls2' c.StodV#c.Kvinna c.StodV#c.Gift_InvAr c.StodV#c.UtbAr_InvAr c.StodV#c.Barn0_15_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.StodV InvAr##c.StodV IFAUkod##c.StodV Ar##c.StodV FodelseAr##c.StodV) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  
  esttab ca1 ca2 ca3 ca4 using "`output'\Output\TableA7_A.rtf", keep(`controls2' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
  
 //######################### Divide the results by the size of NyD in 1991 #################################// 
			
// Regression results Table 				
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  gen aStodNyD = (nydrostkval > 4.4) if nydrostkval <. & Ar == 1991 & `sample'
  bysort kkod: egen StodNyD = max(aStodNyD)
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodNyD == 0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cb1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodNyD == 1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo cb2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodNyD == 0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cb3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & StodNyD == 1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cb4
  sum Nominerad if e(sample)==1
    
   // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  capture drop intT intI 
  gen intT = logpEnklavVuxen*StodNyD
  gen intI = logpEnklavVuxenInvAr*StodNyD
  
  reghdfe Nominerad  `controls2' c.StodNyD#c.Kvinna c.StodNyD#c.Gift_InvAr c.StodNyD#c.UtbAr_InvAr c.StodNyD#c.Barn0_15_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.StodNyD InvAr##c.StodNyD IFAUkod##c.StodNyD Ar##c.StodNyD FodelseAr##c.StodNyD) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  esttab cb1 cb2 cb3 cb4 using "`output'\Output\TableA7_B.rtf", keep(`controls2' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
  
 //########################## Divide the results by the disproportionality #############################//
 
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  sum dispropo if `sample', detail
  gen Disp = disprop > r(p50) if disprop <.
 
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & Disp == 0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cc1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & Disp == 1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo cc2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & Disp == 0, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cc3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & Disp == 1, absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo cc4
  sum Nominerad if e(sample)==1
    
 // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  capture drop intT intI
  gen intT = logpEnklavVuxen*Disp
  gen intI = logpEnklavVuxenInvAr*Disp
  
  reghdfe Nominerad  `controls2' c.Disp#c.Kvinna c.Disp#c.Gift_InvAr c.Disp#c.UtbAr_InvAr c.Disp#c.Barn0_15_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.Disp InvAr##c.Disp IFAUkod##c.Disp Ar##c.Disp FodelseAr##c.Disp) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  esttab cc1 cc2 cc3 cc4 using "`output'\Output\TableA7_C.rtf", keep(`controls2' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
  
  
  est restore ca3
  gen b = _b[logpEnklavVuxen] in 1
  gen lci = b-1.96*_se[logpEnklavVuxen] in 1 
  gen hci = b+1.96*_se[logpEnklavVuxen] in 1
  
  est restore ca4
  replace b = _b[logpEnklavVuxen] in 2
  replace lci = b-1.96*_se[logpEnklavVuxen] in 2 
  replace hci = b+1.96*_se[logpEnklavVuxen] in 2
  
  est restore cb3
  replace b = _b[logpEnklavVuxen] in 3
  replace lci = b-1.96*_se[logpEnklavVuxen] in 3 
  replace hci = b+1.96*_se[logpEnklavVuxen] in 3
  
  est restore cb4
  replace b = _b[logpEnklavVuxen] in 4
  replace lci = b-1.96*_se[logpEnklavVuxen] in 4 
  replace hci = b+1.96*_se[logpEnklavVuxen] in 4
  
  est restore cc3
  replace b = _b[logpEnklavVuxen] in 5
  replace lci = b-1.96*_se[logpEnklavVuxen] in 5 
  replace hci = b+1.96*_se[logpEnklavVuxen] in 5
  
  est restore cc4
  replace b = _b[logpEnklavVuxen] in 6
  replace lci = b-1.96*_se[logpEnklavVuxen] in 6 
  replace hci = b+1.96*_se[logpEnklavVuxen] in 6
  
  gen hid = .
  replace hid = 1 in 1
  replace hid = 1.5 in 2
  replace hid = 2.5 in 3
  replace hid = 3 in 4
  replace hid = 4 in 5
  replace hid = 4.5 in 6
  
  twoway (rcap lci hci hid if inrange(hid, 1, 4.5), lcol(black)) (scatter b hid if inrange(hid, 1, 4.5), legend(off) xtitle("") mcol(black) ///
          xscale(range(.5 5)) xlab(1 "    Low" 1.5 "    High" 2.5 "    Low" 3 "    High" 4 "    Low" 4.5 "    High", angle(45) labsize(small)) ///
		  text(-1.25 1.25 "Left party strength", place(c) color(black)) text(-1.25 2.75 "Right populist strength", place(c) color(black)) ///
		  text(-1.25 4.25 "Disproportionality", place(c) color(black)) graphregion(color(white)) ylab(-1(.25).5))
  
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\Figure3.png", as(png) replace    
  
 //################# Results by sex  ###############################/
   local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
 
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if Kvinna == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hs1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if Kvinna == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hs2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if Kvinna == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hs3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if Kvinna == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hs4
  sum Nominerad if e(sample)==1
  
 // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  capture drop intT intI
  gen intT = logpEnklavVuxen*Kvinna
  gen intI = logpEnklavVuxenInvAr*Kvinna
  
  reghdfe Nominerad  `controls2' c.Kvinna#c.Gift_InvAr c.Kvinna#c.UtbAr_InvAr c.Kvinna#c.Barn0_15_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.Kvinna InvAr##c.Kvinna IFAUkod##c.Kvinna Ar##c.Kvinna FodelseAr##c.Kvinna) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  esttab hs1 hs2 hs3 hs4 using "`output'\Output\TableA8_A.rtf", keep(`controls2' logpEnklavVuxen) replace star(* .1 ** .05 *** .01) b(3) se(3)
 
 //################# Results by education  ###############################/
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  gen HUtb = UtbAr_InvAr > 10 if UtbAr_InvAr <.
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if HUtb == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hu1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if HUtb == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hu2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if HUtb == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hu3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if HUtb == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hu4
  sum Nominerad if e(sample)==1
  
  // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  capture drop intT intI
  gen intT = logpEnklavVuxen*HUtb
  gen intI = logpEnklavVuxenInvAr*HUtb
  
  reghdfe Nominerad  `controls2' c.HUtb#c.Gift_InvAr c.Kvinna#c.HUtb c.HUtb#c.Barn0_15_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.HUtb InvAr##c.HUtb IFAUkod##c.HUtb Ar##c.HUtb FodelseAr##c.HUtb) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  esttab hu1 hu2 hu3 hu4 using "`output'\Output\TableA8_B.rtf", keep(`controls2' logpEnklavVuxen) replace star(* .1 ** .05 *** .01) b(3) se(3)
 
  //################# Results by citizenship status  ###############################/
   local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1 "
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if SvMed == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hc1
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if SvMed == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hc2
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if SvMed == 0 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hc3
  sum Nominerad if e(sample)==1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if SvMed == 1 & `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo hc4
  sum Nominerad if e(sample)==1
  
  // Code to significance the difference, use a fully interacted model to replicate the split sample results 
  capture drop intT intI
  gen intT = logpEnklavVuxen*SvMed
  gen intI = logpEnklavVuxenInvAr*SvMed
  
  reghdfe Nominerad  `controls2' c.SvMed#c.Gift_InvAr c.Kvinna#c.SvMed c.SvMed#c.Barn0_15_InvAr c.SvMed#c.UtbAr_InvAr (logpEnklavVuxen intT = logpEnklavVuxenInvAr intI) if `sample', absorb(InKommun##c.SvMed InvAr##c.SvMed IFAUkod##c.SvMed Ar##c.SvMed FodelseAr##c.SvMed) cluster(InKommun) stages(first) old
  eststo ca1
  sum Nominerad if e(sample)==1
  
  lincom _b[intT]
  lincom _b[logpEnklavVuxen]
  lincom _b[logpEnklavVuxen]+_b[intT]
  
  esttab hc1 hc2 hc3 hc4 using "`output'\Output\TableA8_C.rtf", keep(`controls2' logpEnklavVuxen) replace star(* .1 ** .05 *** .01) b(3) se(3)
 
  log close 
